<template>
  <div class="app-container" style="font-size: 14px;font-weight: 500">
    <div class="filter-container">
      <el-input v-model="listQuery.title" :placeholder="$t('number.title_placeholder')" style="width: 200px;"
                class="filter-item"/>
      <el-input v-model="listQuery.city" :placeholder="$t('number.city_placeholder')"
                style="width: 200px;margin-left: 20px"
                class="filter-item"/>
      <el-date-picker v-model:start-date="listQuery.start_create_at"
                      type="date"
                      format="yyyy-MM-dd"
                      :placeholder="$t('number.created_at_start_placeholder')"
                      style="width: 200px;margin-left: 20px"
      />
      至
      <el-date-picker v-model:start-date="listQuery.end_create_at"
                      type="date"
                      format="yyyy-MM-dd"
                      :placeholder="$t('number.created_at_end_placeholder')"
      />
      <el-button  class="filter-item" type="primary" icon="el-icon-search" @click="getList()"
                 style="margin-left: 20px">
        {{ $t('table.search') }}
      </el-button>
      <el-button type="primary" @click="hrefTo">
        <svg-icon icon-class="add_table" class-name="card-panel-icon icon-refresh"/>
      </el-button>
      <el-button type="primary" @click="getList">
        <svg-icon icon-class="refresh" class-name="card-panel-icon icon-refresh"/>
      </el-button>
    </div>

    <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :element-loading-text="$t('table.load')"
        :data="list"
        border
        fit
        size="medium"
        highlight-current-row
        @selection-change="handleSelectionChange"
        style="margin-top: 20px"
    >

      <el-table-column type="selection" align="center"/>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('number.title')">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('number.country_city')">
        <template slot-scope="scope">
          {{ scope.row.country_city }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('number.city_code')">
        <template slot-scope="scope">
          {{ scope.row.city_code }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('number.total_number')">
        <template slot-scope="scope">
          {{ scope.row.total_number }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('number.total_price')">
        <template slot-scope="scope">
          {{ scope.row.total_price }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.created_at')">
        <template slot-scope="scope">
          {{ scope.row.created_at }}
        </template>
      </el-table-column>
      <el-table-column prop="status" :label="$t('task.status')" :formatter="statusFormat" width="150"/>
      <el-table-column :label="$t('table.actions')" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="row.send_status === 3 ">
            <el-button type="primary" size="mini" @click="downloadFile(row)">
              {{ $t('route.down') }}
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"/>
  </div>
</template>

<script>
import {genList} from '@/api/generate'
import Pagination from '@/components/Pagination'

export default {
  name: 'SelectExcel',
  components: {Pagination},
  data() {
    return {
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: "",
        city: "",
        start_create_at: "",
        end_create_at: "",
        sort: '+id'
      },
      listLoading: true,
      multipleSelection: [],
      downloadLoading: false,
      filename: '',
      taskId: '',
      logData: [],
      logTotal: 0,
      logListQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      logListLoading: false,
      dialogPvVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    downloadFile(row) {
      const url = row.down_url; // 文件的 URL 地址
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', ''); // 设置文件属性为下载
      document.body.appendChild(link); // 将元素插入到 DOM 中
      link.click(); // 模拟点击下载链接进行下载
      document.body.removeChild(link); // 下载完成后移除元素
    },

    getList() {
      this.listLoading = true
      genList({
        page: this.listQuery.page,
        limit: this.listQuery.limit,
        city: this.listQuery.city,
        title: this.listQuery.title,
        start_create_at: this.listQuery.start_create_at,
        end_create_at: this.listQuery.end_create_at,
      }).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    hrefTo() {
      this.$router.push('/number/create')
    },
    statusFormat(row, column, cellValue, index) {
      return {0: '等待执行', 1: '生成中', 2: '生成中', 3: '完成'}[cellValue]
    },
  }
}
</script>
<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 120px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 28%;
}

.icon-refresh {
  color: #ffffff;
}
</style>
